﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DataAccess.SingleSignOn;
using Single_SignOn.Areas.Handheld.Models;
using UserProfile = Single_SignOn.Models.UserProfile;

namespace Single_SignOn.Areas.Handheld.Controllers
{
    public class ApplicationListController : Controller
    {
        //
        // GET: /Handheld/ApplicationList/
        public AuthenConnection oAuth;

        public ApplicationListModels _applicationmodel;


        public FunctionListModels _functionmodel;


        public ActionResult Index()
        {
            if (Request.QueryString["company"] != null && Request.QueryString["language"] != null)
            {
                string company = Request.QueryString["company"];

                string language = Request.QueryString["language"];

                return ChangeLanguage(language, company);
            }
            else
            {
                oAuth = new AuthenConnection();

                ApplicationListModels model = GetListModels();

                ViewBag.CompanyName = model.Companies[0].Name;

                return View("Index", model);
            }
        }

        public ActionResult GetFunction(string applno, string company, string language)
        {

            return RedirectToAction("Index", "FunctionList", new { comp = company, lang = language, appl = applno });

        }

        public ActionResult ChangeLanguage(String language, String company)
        {
            /**/
            System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(language); ;
            System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
            System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.Calendar
                = System.Threading.Thread.CurrentThread.CurrentCulture.Calendar;

            oAuth = new AuthenConnection();

            ApplicationListModels model = GetListModels(company);

            ViewBag.CompanyName = company;
            
            ((UserProfile)Session["UserInfo"]).Language = language;

            return View("Index", model);
        }

        public ActionResult ChangeCompany(string company)
        {

            oAuth = new AuthenConnection();

            ApplicationListModels model = GetListModels(company);

            ViewBag.CompanyName = company;
            
            return View("Index", model);
        }

        protected ApplicationListModels GetListModels(String comp = null)
        {
            _applicationmodel = new ApplicationListModels();
            {
                oAuth = new AuthenConnection();

                // get Company
                DataTable dtCompany = oAuth.getCompany((string)((UserProfile)Session["UserInfo"]).Username);
                Company company;
                if (dtCompany.Rows.Count > 0)
                {
                    List<Company> Companies = new List<Company>();
                    for (int i = 0; i < dtCompany.Rows.Count; i++)
                    {
                        company = new Company();
                        company.Name = (string)dtCompany.Rows[i]["company_code"];
                        company.Description = (string)dtCompany.Rows[i]["company_name_en"];
                        Companies.Add(company);
                    }
                    _applicationmodel.Companies = Companies;
                }

                // get Language
                DataTable dtLanguage = oAuth.getLanguage();
                Language language;
                if (dtLanguage.Rows.Count > 0)
                {
                    List<Language> languages = new List<Language>();
                    for (int i = 0; i < dtLanguage.Rows.Count; i++)
                    {
                        language = new Language();
                        language.Name = (string)dtLanguage.Rows[i]["lang_culture_name"];
                        language.Description = (string)dtLanguage.Rows[i]["lang_name"];
                        languages.Add(language);
                    }
                    _applicationmodel.Languages = languages;
                }

                //get Application List
                DataTable dtApplication =
                    oAuth.getApplicationListWithHandHeld((string)((UserProfile)Session["UserInfo"]).Username,
                    (String.IsNullOrEmpty(comp)
                    ? _applicationmodel.Companies[0].Name
                    : comp));

                ApplicationList application;
                if (dtApplication.Rows.Count > 0)
                {
                    List<ApplicationList> applicationLists = new List<ApplicationList>();
                    for (int i = 0; i < dtApplication.Rows.Count; i++)
                    {
                        application = new ApplicationList();
                        application.No = i + 1;
                        application.ApplNo = (string)dtApplication.Rows[i]["applno"];
                        application.Application = (string)dtApplication.Rows[i]["applname"];
                        application.Description = (string)dtApplication.Rows[i]["description"];
                        application.Url = (string)dtApplication.Rows[i]["url"];
                        applicationLists.Add(application);
                    }
                    _applicationmodel.ApplicationLists = applicationLists;
                }
            }
            return _applicationmodel;
        }

       
    }
}
